package com.example.lemis.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.common.exception.IllegalArgException;
import com.example.lemis.mapper.DoorplateAuditMapper;
import com.example.lemis.mapper.DoorplateMapper;
import com.example.lemis.model.domain.Doorplate;
import com.example.lemis.model.domain.DoorplateApply;
import com.example.lemis.mapper.DoorplateApplyMapper;
import com.example.lemis.model.dto.DoorplateApplyDTO;
import com.example.lemis.model.dto.DoorplateApprovalDTO;
import com.example.lemis.model.dto.DoorplateReissueDTO;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.vo.AdminInfoVO;
import com.example.lemis.model.vo.DoorplateApplyVO;
import com.example.lemis.model.vo.DoorplateVO;
import com.example.lemis.model.vo.UserInfoVO;
import com.example.lemis.service.IAdminService;
import com.example.lemis.service.IDoorplateApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lemis.service.IDoorplateService;
import com.example.lemis.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 管理门牌每次审批数据 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-06-24
 */
@AllArgsConstructor
@Service
public class DoorplateApplyServiceImpl extends ServiceImpl<DoorplateApplyMapper, DoorplateApply> implements IDoorplateApplyService {
    private final DoorplateApplyMapper doorplateApplyMapper;
    private final IUserService iUserService;

    private final DoorplateMapper doorplateMapper;
    private final DoorplateAuditMapper doorplateAuditMapper;
    private final IAdminService iAdminService;
    private final IDoorplateService iDoorplateService;

    @Override
    public Page<DoorplateApplyVO> queryApplyList(List<Long> streetIds, PageDTO pageDTO) {
        AdminInfoVO admin = iAdminService.auth();
        Page<DoorplateApplyVO> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());

        // 获取当前用户的权限
        List<String> perms = iAdminService.checkApplyPerm(admin.getId(), "", false);
        return doorplateApplyMapper.queryApplyList(page, streetIds, perms);
    }

    @Override
    public DoorplateApplyVO insertApply(DoorplateApplyDTO dto) {
        // 这里可以添加业务逻辑，比如保存到数据库等
        // 假设我们直接返回传入的对象作为示例
        UserInfoVO user = iUserService.auth();
        final Long DOORPLATE_APPLY_STATUS = 0L;
        doorplateApplyMapper.insertApply(dto, user.getId(), DOORPLATE_APPLY_STATUS);
        // 返回一个 DoorplateApplyVO 对象

        return doorplateApplyMapper.selectByIdWithClazzStreet(dto.getId(),
                dto.getClazzId(),
                dto.getStreetId());

    }

    @Override
    public DoorplateApplyVO reissueApply(DoorplateReissueDTO doorplateReissue) {
        UserInfoVO user = iUserService.auth();
        Doorplate doorplate = doorplateMapper.selectById(doorplateReissue.getDoorplateId());
        if (doorplate == null || !Objects.equals(user.getId(), doorplate.getUserId())) {
            return null;
        }
        doorplateApplyMapper.reissueApply(doorplateReissue, doorplate);
        return doorplateApplyMapper.selectByIdWithClazzStreet(doorplateReissue.getId(),
                doorplate.getClazzId(),
                doorplate.getStreetId());
    }

    @Override
    public DoorplateApplyVO approval(Long applyId, DoorplateApprovalDTO dto) {
        AdminInfoVO admin = iAdminService.auth();
        DoorplateApply apply = doorplateApplyMapper.selectById(applyId); // 获取申请信息
        Long maxLevel = apply.getLevels(); // 获取最大审批级别
        Long currentLevel = doorplateAuditMapper.selectCurrentLevelByApplyId(applyId); // 获取当前已通过审批最大级别

        // 检查操作合法性
        if (Objects.equals(currentLevel, maxLevel)) {
            throw new IllegalArgException("当前审批级别已超过最大级别");
        }
        iAdminService.checkApplyPerm(admin.getId(), "apply_" + (currentLevel + 1), true);

        doorplateAuditMapper.insertAudit(dto, applyId, admin.getId(), currentLevel + 1);

        if (dto.getResult() == 0) { // 同意
            if (currentLevel + 1 == maxLevel) {
                DoorplateVO doorplate = iDoorplateService.insert(apply.toDoorplateInsertDTO());
                apply.setDoorplateId(doorplate.getId());
                doorplateApplyMapper.updateById(apply);
            }
        } else {
            // 拒绝申请
            apply.setStatus(1L); // 设置状态为拒绝
            doorplateApplyMapper.updateById(apply);
        }
        return doorplateApplyMapper.selectByIdWithClazzStreet(apply.getId(),
                apply.getClazzId(),
                apply.getStreetId());
    }
    
    @Override
    public DoorplateApply selectNearApply(DoorplateReissueDTO doorplateReissue) {
        return doorplateApplyMapper.selectNearApply(doorplateReissue);
    }
}
